package zong.Listener;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class MyFrame extends JFrame implements ActionListener {
   JButton jtb1=new JButton("点我啊");
    JButton jtb2=new JButton("再点我啊");

    public MyFrame() {

        this.setSize(500, 500);
        this.setTitle("事件演示");
        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);


        //创建按钮对象

        jtb1.setBounds(0, 10, 100, 30);
        jtb1.addActionListener(this);


        jtb2.setBounds(100, 10, 100, 30);
        jtb2.addActionListener(this);

        this.getContentPane().add(jtb1);//把按钮添加到界面
        this.getContentPane().add(jtb2);//把按钮添加到界面






        this.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
//对当前按钮进行判断
        //获取当前被操作的那个按钮对象
        Object source=e.getSource();
        if(source==jtb1){
            jtb1.setSize(20,200);
        }else if(source==jtb2){
            Random r=new Random();
            jtb2.setLocation(r.nextInt(465),r.nextInt(543));
        }
    }
}
